{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "35020669",
   "metadata": {},
   "source": [
    "### 检验指标确定"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "02daeff1",
   "metadata": {},
   "outputs": [],
   "source": [
    "#一类指标：人均活跃时长\n",
    "#二类指标：人均点击率=点击人数/曝光人数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ded3040b",
   "metadata": {},
   "source": [
    "### 确定检验统计量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0ac42519",
   "metadata": {
    "code_folding": []
   },
   "outputs": [],
   "source": [
    "#一类指标：人均活跃时长之差μA-μB\n",
    "#二类指标：人均点击率之差PA-PB"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5cb345e7",
   "metadata": {},
   "source": [
    "### 埋点收集数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3fb13120",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'user_id': '用户ID',\n",
       " 'timestamp': '用户banner页浏览时间',\n",
       " 'group': '用户分组',\n",
       " 'landing_page': '新老banner主色调',\n",
       " 'converted': '是否点击'}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "maidian={'user_id':'用户ID',\n",
    "         'timestamp':'用户banner页浏览时间',\n",
    "         'group':'用户分组',\n",
    "         'landing_page':'新老banner主色调',\n",
    "         'converted':'是否点击'}\n",
    "maidian"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56d46a7a",
   "metadata": {},
   "source": [
    "### 确定H0、H1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f77cc48",
   "metadata": {},
   "source": [
    "#### 一类指标：人均活跃时长之差μA-μB小于2*σ"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b83a7579",
   "metadata": {},
   "outputs": [],
   "source": [
    "#H0：μA-μB >= 2*σ\n",
    "#H1：μA-μB < 2*σ"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f1a30359",
   "metadata": {},
   "source": [
    "#### 二类指标：人均点击率之差PA-PB大于0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4e5fd9b0",
   "metadata": {},
   "outputs": [],
   "source": [
    "#H0：PA-PB < 0\n",
    "#H1：PA-PB > 0"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "618247a0",
   "metadata": {},
   "source": [
    "### 确定显著性水平α"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "16c2d03e",
   "metadata": {},
   "outputs": [],
   "source": [
    "#一类错误：默认α=0.05\n",
    "#二类错误：默认β=0.2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d809faf4",
   "metadata": {},
   "source": [
    "### 计算样本量"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "95e3a251",
   "metadata": {},
   "source": [
    "#### 一类指标样本量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "bc2d70c6",
   "metadata": {},
   "outputs": [],
   "source": [
    "#无数据"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "091e226d",
   "metadata": {},
   "source": [
    "#### 二类指标样本量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "f9555368",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy import stats\n",
    "import pymysql\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "plt.rcParams['font.family']='SimHei'\n",
    "plt.rcParams['axes.unicode_minus']= False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "42d7162a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>timestamp</th>\n",
       "      <th>group</th>\n",
       "      <th>landing_page</th>\n",
       "      <th>converted</th>\n",
       "      <th>date</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>851104</td>\n",
       "      <td>2017-01-21 22:11:48.556739</td>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>0</td>\n",
       "      <td>2017-01-21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>804228</td>\n",
       "      <td>2017-01-12 08:01:45.159739</td>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>0</td>\n",
       "      <td>2017-01-12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>661590</td>\n",
       "      <td>2017-01-11 16:55:06.154213</td>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>0</td>\n",
       "      <td>2017-01-11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>853541</td>\n",
       "      <td>2017-01-08 18:28:03.143765</td>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>0</td>\n",
       "      <td>2017-01-08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>864975</td>\n",
       "      <td>2017-01-21 01:52:26.210827</td>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>1</td>\n",
       "      <td>2017-01-21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>294473</th>\n",
       "      <td>751197</td>\n",
       "      <td>2017-01-03 22:28:38.630509</td>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>0</td>\n",
       "      <td>2017-01-03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>294474</th>\n",
       "      <td>945152</td>\n",
       "      <td>2017-01-12 00:51:57.078372</td>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>0</td>\n",
       "      <td>2017-01-12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>294475</th>\n",
       "      <td>734608</td>\n",
       "      <td>2017-01-22 11:45:03.439544</td>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>0</td>\n",
       "      <td>2017-01-22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>294476</th>\n",
       "      <td>697314</td>\n",
       "      <td>2017-01-15 01:20:28.957438</td>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>0</td>\n",
       "      <td>2017-01-15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>294477</th>\n",
       "      <td>715931</td>\n",
       "      <td>2017-01-16 12:40:24.467417</td>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>0</td>\n",
       "      <td>2017-01-16</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>294478 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        user_id                   timestamp      group landing_page  \\\n",
       "0        851104  2017-01-21 22:11:48.556739    control     old_page   \n",
       "1        804228  2017-01-12 08:01:45.159739    control     old_page   \n",
       "2        661590  2017-01-11 16:55:06.154213  treatment     new_page   \n",
       "3        853541  2017-01-08 18:28:03.143765  treatment     new_page   \n",
       "4        864975  2017-01-21 01:52:26.210827    control     old_page   \n",
       "...         ...                         ...        ...          ...   \n",
       "294473   751197  2017-01-03 22:28:38.630509    control     old_page   \n",
       "294474   945152  2017-01-12 00:51:57.078372    control     old_page   \n",
       "294475   734608  2017-01-22 11:45:03.439544    control     old_page   \n",
       "294476   697314  2017-01-15 01:20:28.957438    control     old_page   \n",
       "294477   715931  2017-01-16 12:40:24.467417  treatment     new_page   \n",
       "\n",
       "        converted        date  \n",
       "0               0  2017-01-21  \n",
       "1               0  2017-01-12  \n",
       "2               0  2017-01-11  \n",
       "3               0  2017-01-08  \n",
       "4               1  2017-01-21  \n",
       "...           ...         ...  \n",
       "294473          0  2017-01-03  \n",
       "294474          0  2017-01-12  \n",
       "294475          0  2017-01-22  \n",
       "294476          0  2017-01-15  \n",
       "294477          0  2017-01-16  \n",
       "\n",
       "[294478 rows x 6 columns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.read_csv(r\"C:\\Users\\Xl\\Desktop\\阶段七模块2作业\\ab_data.csv\")\n",
    "data[\"date\"] = data.timestamp.str[:10]\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "0b81895c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数据是否存在重复值\n",
    "data.duplicated().sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "971c9178",
   "metadata": {},
   "outputs": [],
   "source": [
    "alpha = 0.05\n",
    "beta = 0.2\n",
    "k =1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "8aa9d4f7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.1303863045004612 -0.010000000000000009\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "13557.05945906891"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#求P_A\n",
    "treatment_p = data.converted[(data.group==\"control\")&(data.landing_page==\"old_page\")].mean()\n",
    "treatment_p\n",
    "#求P_A*(1-P_A)\n",
    "treatment_p_1_pa = treatment_p * (1 - treatment_p)\n",
    "treatment_p_1_pa\n",
    "# 均值之差在大样本下其分布近似正态分布\n",
    "#求Z_(1-α)\n",
    "z_alpha = stats.norm.isf(alpha,loc=0,scale=1)\n",
    "z_alpha\n",
    "#求Z_(1-β)\n",
    "z_beta = stats.norm.isf(beta,loc=0,scale=1)\n",
    "z_beta\n",
    "#求P_B\n",
    "#H1为 Pa-Pb>0，为计算样本量，这里需要指定一个非0的值， 此值的绝对值越小样本量越大\n",
    "#我们先指定一个偏小的值 0.01\n",
    "control_p = treatment_p + 0.01\n",
    "p0 = treatment_p - control_p\n",
    "print(control_p,p0)\n",
    "#求P_B*(1-P_B)\n",
    "control_p_1_pb = control_p * (1 - control_p)\n",
    "control_p_1_pb\n",
    "n = (treatment_p_1_pa + control_p_1_pb) * ((z_alpha + z_beta)/(treatment_p - control_p)) ** 2\n",
    "n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "039bf6fd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>group</th>\n",
       "      <th>landing_page</th>\n",
       "      <th>date</th>\n",
       "      <th>user_id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>2017-01-02</td>\n",
       "      <td>2859</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>2017-01-03</td>\n",
       "      <td>6590</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>2017-01-04</td>\n",
       "      <td>6578</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>2017-01-05</td>\n",
       "      <td>6427</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>2017-01-06</td>\n",
       "      <td>6606</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>2017-01-07</td>\n",
       "      <td>6604</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>2017-01-08</td>\n",
       "      <td>6687</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>2017-01-09</td>\n",
       "      <td>6628</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>2017-01-10</td>\n",
       "      <td>6654</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>2017-01-11</td>\n",
       "      <td>6688</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>2017-01-12</td>\n",
       "      <td>6522</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>2017-01-13</td>\n",
       "      <td>6552</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>35</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>2017-01-14</td>\n",
       "      <td>6548</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>36</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>2017-01-15</td>\n",
       "      <td>6714</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>37</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>2017-01-16</td>\n",
       "      <td>6591</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>38</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>2017-01-17</td>\n",
       "      <td>6617</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>39</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>2017-01-18</td>\n",
       "      <td>6482</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>40</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>2017-01-19</td>\n",
       "      <td>6578</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>41</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>2017-01-20</td>\n",
       "      <td>6534</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>42</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>2017-01-21</td>\n",
       "      <td>6749</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>43</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>2017-01-22</td>\n",
       "      <td>6596</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>44</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>2017-01-23</td>\n",
       "      <td>6716</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>45</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>2017-01-24</td>\n",
       "      <td>3754</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>46</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>2017-01-02</td>\n",
       "      <td>2853</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>47</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>2017-01-03</td>\n",
       "      <td>6618</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>48</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>2017-01-04</td>\n",
       "      <td>6541</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>49</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>2017-01-05</td>\n",
       "      <td>6505</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>2017-01-06</td>\n",
       "      <td>6747</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>51</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>2017-01-07</td>\n",
       "      <td>6609</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>52</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>2017-01-08</td>\n",
       "      <td>6700</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>53</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>2017-01-09</td>\n",
       "      <td>6615</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>54</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>2017-01-10</td>\n",
       "      <td>6696</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>55</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>2017-01-11</td>\n",
       "      <td>6673</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>56</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>2017-01-12</td>\n",
       "      <td>6637</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>57</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>2017-01-13</td>\n",
       "      <td>6508</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>58</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>2017-01-14</td>\n",
       "      <td>6600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>59</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>2017-01-15</td>\n",
       "      <td>6549</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>60</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>2017-01-16</td>\n",
       "      <td>6545</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>61</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>2017-01-17</td>\n",
       "      <td>6538</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>62</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>2017-01-18</td>\n",
       "      <td>6603</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>63</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>2017-01-19</td>\n",
       "      <td>6552</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>64</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>2017-01-20</td>\n",
       "      <td>6679</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>65</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>2017-01-21</td>\n",
       "      <td>6560</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>66</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>2017-01-22</td>\n",
       "      <td>6669</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>67</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>2017-01-23</td>\n",
       "      <td>6633</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>68</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>2017-01-24</td>\n",
       "      <td>3681</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        group landing_page        date  user_id\n",
       "23    control     old_page  2017-01-02     2859\n",
       "24    control     old_page  2017-01-03     6590\n",
       "25    control     old_page  2017-01-04     6578\n",
       "26    control     old_page  2017-01-05     6427\n",
       "27    control     old_page  2017-01-06     6606\n",
       "28    control     old_page  2017-01-07     6604\n",
       "29    control     old_page  2017-01-08     6687\n",
       "30    control     old_page  2017-01-09     6628\n",
       "31    control     old_page  2017-01-10     6654\n",
       "32    control     old_page  2017-01-11     6688\n",
       "33    control     old_page  2017-01-12     6522\n",
       "34    control     old_page  2017-01-13     6552\n",
       "35    control     old_page  2017-01-14     6548\n",
       "36    control     old_page  2017-01-15     6714\n",
       "37    control     old_page  2017-01-16     6591\n",
       "38    control     old_page  2017-01-17     6617\n",
       "39    control     old_page  2017-01-18     6482\n",
       "40    control     old_page  2017-01-19     6578\n",
       "41    control     old_page  2017-01-20     6534\n",
       "42    control     old_page  2017-01-21     6749\n",
       "43    control     old_page  2017-01-22     6596\n",
       "44    control     old_page  2017-01-23     6716\n",
       "45    control     old_page  2017-01-24     3754\n",
       "46  treatment     new_page  2017-01-02     2853\n",
       "47  treatment     new_page  2017-01-03     6618\n",
       "48  treatment     new_page  2017-01-04     6541\n",
       "49  treatment     new_page  2017-01-05     6505\n",
       "50  treatment     new_page  2017-01-06     6747\n",
       "51  treatment     new_page  2017-01-07     6609\n",
       "52  treatment     new_page  2017-01-08     6700\n",
       "53  treatment     new_page  2017-01-09     6615\n",
       "54  treatment     new_page  2017-01-10     6696\n",
       "55  treatment     new_page  2017-01-11     6673\n",
       "56  treatment     new_page  2017-01-12     6637\n",
       "57  treatment     new_page  2017-01-13     6508\n",
       "58  treatment     new_page  2017-01-14     6600\n",
       "59  treatment     new_page  2017-01-15     6549\n",
       "60  treatment     new_page  2017-01-16     6545\n",
       "61  treatment     new_page  2017-01-17     6538\n",
       "62  treatment     new_page  2017-01-18     6603\n",
       "63  treatment     new_page  2017-01-19     6552\n",
       "64  treatment     new_page  2017-01-20     6679\n",
       "65  treatment     new_page  2017-01-21     6560\n",
       "66  treatment     new_page  2017-01-22     6669\n",
       "67  treatment     new_page  2017-01-23     6633\n",
       "68  treatment     new_page  2017-01-24     3681"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看我们的样本量是否满足最低样本量\n",
    "dd = data.groupby([\"group\",\"landing_page\",\"date\"],as_index=False)[\"user_id\"].count()\n",
    "dd[((dd.group == \"control\") & (dd.landing_page==\"old_page\"))|((dd.group==\"treatment\")&(dd.landing_page==\"new_page\"))]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9badcb0e",
   "metadata": {},
   "source": [
    "### 利用统计工具实现检验"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "cdb5d6b8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>group</th>\n",
       "      <th>landing_page</th>\n",
       "      <th>converted</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>control</td>\n",
       "      <td>new_page</td>\n",
       "      <td>0.106383</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>0.113809</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>0.113781</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>treatment</td>\n",
       "      <td>old_page</td>\n",
       "      <td>0.097826</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       group landing_page  converted\n",
       "0    control     new_page   0.106383\n",
       "1    control     old_page   0.113809\n",
       "2  treatment     new_page   0.113781\n",
       "3  treatment     old_page   0.097826"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = data[data.date==\"2017-01-03\"].groupby([\"group\",\"landing_page\"],as_index=False)[\"converted\"].mean()\n",
    "df\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "ef049388",
   "metadata": {},
   "outputs": [],
   "source": [
    "#求控制组的旧页面和对照组的新页面比例之差\n",
    "statistic_t = df.converted[2] - df.converted[1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "2f514766",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6618 6590\n"
     ]
    }
   ],
   "source": [
    "n_B =data[data.date=='2017-01-03'].converted[(data.group=='control')&(data.landing_page=='old_page')].size\n",
    "n_A =data[data.date=='2017-01-03'].converted[(data.group=='treatment')&(data.landing_page=='new_page')].size\n",
    "print(n_A,n_B)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "ec0e0fc4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.005526379176809786"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sigma = np.sqrt(df.converted[2]*(1-df.converted[2])/n_A + df.converted[1]*(1-df.converted[1])/n_B)\n",
    "sigma\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "680d1d3a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.5020359187180234"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "statistic_p = 1-stats.norm.cdf(statistic_t,0,sigma)\n",
    "statistic_p\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "196b43ad",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "显著性P>α，实验组点击率<对照组\n"
     ]
    }
   ],
   "source": [
    "if statistic_p > alpha:\n",
    "    print(\"显著性P>α，实验组点击率<对照组\")\n",
    "else:\n",
    "    print(\"显著性P<α，实验组点击率>对照组\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "ab47e449",
   "metadata": {},
   "outputs": [],
   "source": [
    "#封装函数\n",
    "\n",
    "def abtest(df: pd.DataFrame, alpha=0.05, group_col: str = None, value_col: str =None):\n",
    "    '''\n",
    "    :param df:被分析DataFrame对象\n",
    "    :param alpha:显著性\n",
    "    :param group_col:组列的名字，默认为df的第一列\n",
    "    :param value_col:值列的名字，默认为df的第二列\n",
    "    :return:tongjiliang p_value p_type\n",
    "    ''' \n",
    "   # 列名\n",
    "    if not group_col:\n",
    "        group_col = df.columns[0]\n",
    "  \n",
    "    if not value_col:\n",
    "        value_col = df.columns[1]\n",
    " \n",
    "    \n",
    "    temp = df.groupby(group_col,as_index=False)[value_col].mean()\n",
    "\n",
    "    temp_n = df.groupby(group_col,as_index=False)[value_col].count()\n",
    "\n",
    "    statistic_t =temp.iloc[0,1] - temp.iloc[1,1] #先求出各组均值在求出统计量差值\n",
    "\n",
    "    sigma =np.sqrt(temp.iloc[0,1] *(1-temp.iloc[0,1])/temp_n.iloc[0,1] + temp.iloc[1,1] *(1-temp.iloc[1,1])/temp_n.iloc[1,1]) #标准差\n",
    "\n",
    "    statistic_l_p = stats.norm.cdf(statistic_t,0,sigma)\n",
    "\n",
    "    statistic_r_p = 1-stats.norm.cdf(statistic_t,0,sigma)\n",
    "\n",
    "    statistic_p = statistic_l_p *2;\n",
    "\n",
    "    if statistic_p >alpha:\n",
    "\n",
    "        statistic_p = statistic_l_p*2\n",
    "\n",
    "    temp_l = [[temp.iloc[0,0],temp.iloc[1,0],statistic_t,\"左侧\",statistic_l_p,np.where(statistic_l_p<alpha,\"显著\",\"不显著\")],[temp.iloc[0,0],temp.iloc[1,0],statistic_t,\"右侧\",statistic_r_p,np.where(statistic_r_p<alpha,\"显著\",\"不显著\")],[temp.iloc[0,0],temp.iloc[1,0],statistic_t,\"双侧\",statistic_p,np.where(statistic_p<alpha,\"显著\",\"不显著\")]]\n",
    "\n",
    "    temp = pd.DataFrame(temp_l,columns =[\"p\",\"p0\",\"统计量\",\"检测\",\"p_value\",\"结果\"])\n",
    "\n",
    "    return temp\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "c68922d1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>group</th>\n",
       "      <th>converted</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>treatment</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>86</th>\n",
       "      <td>treatment</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>treatment</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>169</th>\n",
       "      <td>treatment</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>205</th>\n",
       "      <td>control</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         group  converted\n",
       "8    treatment          1\n",
       "86   treatment          0\n",
       "96   treatment          0\n",
       "169  treatment          0\n",
       "205    control          0"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "temp  = data[data.date=='2017-01-04'].loc[((data.group==\"control\") & (data.landing_page==\"old_page\")) \n",
    "                  | ((data.group==\"treatment\") & (data.landing_page==\"new_page\")),[\"group\",\"converted\"]]\n",
    "temp.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "39ed2549",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>p</th>\n",
       "      <th>p0</th>\n",
       "      <th>统计量</th>\n",
       "      <th>检测</th>\n",
       "      <th>p_value</th>\n",
       "      <th>结果</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>control</td>\n",
       "      <td>treatment</td>\n",
       "      <td>0.005273</td>\n",
       "      <td>左侧</td>\n",
       "      <td>0.824250</td>\n",
       "      <td>不显著</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>control</td>\n",
       "      <td>treatment</td>\n",
       "      <td>0.005273</td>\n",
       "      <td>右侧</td>\n",
       "      <td>0.175750</td>\n",
       "      <td>不显著</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>control</td>\n",
       "      <td>treatment</td>\n",
       "      <td>0.005273</td>\n",
       "      <td>双侧</td>\n",
       "      <td>1.648501</td>\n",
       "      <td>不显著</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         p         p0       统计量  检测   p_value   结果\n",
       "0  control  treatment  0.005273  左侧  0.824250  不显著\n",
       "1  control  treatment  0.005273  右侧  0.175750  不显著\n",
       "2  control  treatment  0.005273  双侧  1.648501  不显著"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "abtest(temp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f6fc8697",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
